import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus/utils/Toast.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  List list = [];

  // @override
  @override
  void initState() {
    super.initState();
    getList();
  }

  getList() async {
    final res = await getAnnounceListApi();
    setState(() {
      list = res;
      PrompAction.showToast('获取成功');
    });
    print(res);
  }

  Timer? timer;
  int count = 60;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(229, 231, 242, 1),
      appBar: AppBar(
        title: const Text('享 + 社区', style: TextStyle(color: Colors.black)),
        centerTitle: true,
        backgroundColor: const Color.fromRGBO(229, 231, 242, 1),
      ),
      body: ListView(padding: const EdgeInsets.all(10), children: [
        ElevatedButton(
            onPressed: () {
              tokenManager.setToken('1313123');
              PrompAction.showToast('设置token成功');
            },
            child: const Text('设置token')),
        const SizedBox(height: 10),
        ElevatedButton(
            onPressed: () async {
              final token = await tokenManager.getToken();
              PrompAction.showToast('获取token成功$token');
            },
            child: const Text('获取token')),
        const SizedBox(height: 10),
        ElevatedButton(
            onPressed: () {
              tokenManager.removeToken();
              PrompAction.showToast('删除token成功');
            },
            child: const Text('删除token')),
        const SizedBox(height: 10),
        Text(
          '秒数$count',
          textAlign: TextAlign.center,
        ),
        ElevatedButton(
            onPressed: () {
              timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                setState(() {
                  count--;
                });
              });
            },
            child: const Text('开启定时器')),
        const SizedBox(height: 10),
        ElevatedButton(
            onPressed: () {
              timer?.cancel();
            },
            child: const Text('关闭定时器')),
        const SizedBox(height: 10),
        ElevatedButton(
            onPressed: () {
              Future.delayed(const Duration(seconds: 3), () {
                PrompAction.showToast('延时3秒');
              });
            },
            child: const Text('延时器')),
        const SizedBox(height: 10),
        HomeNav(),
        ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Image.asset('assets/images/banner@2x.jpg'),
        ),
        HomeList(
          notifyList: list,
        )
      ]),
    );
  }
}
